package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.userInfo.entity.EnshrineInfo;
import com.rongshu.view.userInfo.listener.OnEnshrineItemListener;
import com.rongshu.widget.ArticleLabelView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class EnshrineAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //context
    private Context mContext = null;
    //list
    private List<EnshrineInfo.ResponseBean> enshrineInfos = null;
    //interface
    private OnEnshrineItemListener listener = null;
    //type
    private boolean isVoice = false;

    public EnshrineAdapter(Context context, boolean isVoice) {
        this.mContext = context;
        this.isVoice = isVoice;
    }

    public void setEnshrineInfos(List<EnshrineInfo.ResponseBean> list) {
        this.enshrineInfos = list;
    }

    public void addEnshrineInfos(List<EnshrineInfo.ResponseBean> list) {
        this.enshrineInfos.addAll(list);
    }

    public List<EnshrineInfo.ResponseBean> getEnshrineInfos() {
        return this.enshrineInfos;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_enshrine_item_view, parent, false);
        return new EnshrineViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        final EnshrineInfo.ResponseBean bean = this.enshrineInfos.get(position);
        EnshrineViewHolder holder = (EnshrineViewHolder) viewHolder;
        //image
        String topImg = bean.getTopImg();
        if (TextUtils.isEmpty(topImg)) {
            holder.articleImg.setVisibility(View.GONE);
        } else {
            holder.articleImg.setVisibility(View.VISIBLE);
            //img
            GlideUtils.loadImageView(mContext, holder.articleImg, topImg,
                    R.drawable.default_preview_icon, R.drawable.default_preview_icon);
        }
        //title
        holder.articleTitle.setText(bean.getTitle());
        //content
        holder.articleContent.setText(bean.getContents_header());
        //author
        holder.article_author.setVisibility(isVoice ? View.GONE : View.VISIBLE);
        holder.article_author.setText(bean.getBi_name());
        //category
        holder.articleCategory.setText(bean.getCategories());
        //label
        holder.articleLabel.bindDatas(bean.getKeywordList());
        //listener
        holder.contentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position, bean.getContribute_id());
                }
            }
        });
        //detele
        holder.btn_cancel_collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemCancleEnshrineClick(position, bean.getContribute_id());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return enshrineInfos == null ? 0 : enshrineInfos.size();
    }

    public static class EnshrineViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.article_img)
        ImageView articleImg;
        @BindView(R.id.article_title)
        TextView articleTitle;
        @BindView(R.id.article_content)
        TextView articleContent;
        @BindView(R.id.article_author)
        TextView article_author;
        @BindView(R.id.article_category)
        TextView articleCategory;
        @BindView(R.id.article_label)
        ArticleLabelView articleLabel;
        @BindView(R.id.btn_cancel_collection)
        TextView btn_cancel_collection;
        @BindView(R.id.contentLayout)
        LinearLayout contentLayout;


        public EnshrineViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * set listener
     *
     * @param listener
     */
    public void setOnEnshrineItemListener(OnEnshrineItemListener listener) {
        this.listener = listener;
    }
}
